package com.gxl.butt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.dto.qo.QueryBean;
import com.gxl.butt.dto.vo.TokenBean;
import com.gxl.butt.redis.RedisUtil;
import com.gxl.butt.service.IIPService;
import com.gxl.butt.service.ITokenService;
import com.gxl.butt.service.IkafkaOperatorService;
import com.gxl.butt.utils.IPUtil;
import com.gxl.butt.utils.JSONUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.gxl.butt.value.ErrorMsg.*;
import static com.gxl.butt.value.StaticValue.USER_ID;


/**
 * Backend controller used to monitor and restart the KClient.
 *
 * @author guoxiaolin
 * @since Aug 21, 2015
 *
 */
@Api(value = "butt operator",tags = "butt operator")
@RestController
@RequestMapping(value = "/sys")
public class KfkController {

	private  Logger log = LoggerFactory.getLogger(KfkController.class);

	@Reference(interfaceClass = IkafkaOperatorService.class,timeout = 10000,check = false)
	private IkafkaOperatorService kafkaOperatorService;

	@Reference(interfaceClass = IIPService.class,timeout = 10000,check = false)
	private IIPService iipService;

	@Reference(interfaceClass = ITokenService.class,timeout = 10000,check = false)
	private ITokenService iTokenService;

	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;


	@ApiOperation(value="获取用户消息数量，支付总额，TOPIC数量", notes="获取用户消息数量，支付总额，TOPIC数量")
	@RequestMapping(value = "/getCountOfUser",method = RequestMethod.GET)
	public JSONObject getCountOfUser(){

		Long userid = (Long) request.getSession().getAttribute(USER_ID);

		JSONObject data = null;

		data = kafkaOperatorService.getCountOfUser(userid);

		return  data;

	}

	@ApiOperation(value="获取用户日报表", notes="用户每日消息数量，只查询最新30日")
	@RequestMapping(value = "/getReportInDay",method = RequestMethod.POST)
	public JSONObject getReportInDay(){

		Long userid = (Long) request.getSession().getAttribute(USER_ID);
		JSONObject data = null;

		data = kafkaOperatorService.getReportInDay(userid);

		return  data;

	}

	@ApiOperation(value="获取用户月报表", notes="用户每月消息数量，只查询最新12个月")
	@RequestMapping(value = "/getReportInMouth",method = RequestMethod.POST)
	public JSONObject getReportInMouth(){

		Long userid = (Long) request.getSession().getAttribute(USER_ID);
		JSONObject data = null;

		data = kafkaOperatorService.getReportInMouth(userid);
		return  data;

	}

	@ApiOperation(value="添加ip白名单", notes="添加ip")
	@RequestMapping(value = "/addIp",method = RequestMethod.POST)
	public JSONObject addIp(String ip){

		if (!IPUtil.isIp(ip)){
			return  JSONUtils.getRoot(-1,IP_FORMAT_ERROR,STR_IS_EMPTY);
		}
		Long userid = (Long) request.getSession().getAttribute(USER_ID);
		JSONObject data = null;
		data = iipService.addIp(ip,userid);
		return  data;

	}

	@ApiOperation(value="删除ip白名单", notes="删除ip")
	@RequestMapping(value = "/delIp",method = RequestMethod.POST)
	public JSONObject delIp(Long id){

		JSONObject data = null;

		data = iipService.delIpById(id);

		return  data;

	}

	@ApiOperation(value="查询ip白名单列表", notes="获取白名单IP列表")
	@RequestMapping(value = "/getIpList",method = RequestMethod.POST)
	public JSONObject getIpList(){

		JSONObject data = null;

		data = iipService.getIpList((Long) request.getSession().getAttribute(USER_ID));

		if (data==null){

			return  JSONUtils.getRoot(-1,IP_LIST_IS_NULL,STR_IS_EMPTY);

		}

		return  data;

	}


	@ApiOperation(value="获取凭证token", notes="获取接口请求凭证Token")
	@RequestMapping(value = "/getToken",method = RequestMethod.POST)
	public JSONObject getToken(String account,String password){

		Long userid =null;
		userid = (Long) request.getSession().getAttribute(USER_ID);
		if (userid==null){
			return JSONUtils.getRoot(-1,USER_LOGOUT,STR_IS_EMPTY);
		}
		return iTokenService.getToken(account,password,userid);

	}

	@ApiOperation(value="更新凭证token", notes="更新接口请求凭证Token")
	@RequestMapping(value = "/updateToken",method = RequestMethod.POST)
	public JSONObject updateToken(String account,String password){

		Long userid =null;
		userid = (Long) request.getSession().getAttribute(USER_ID);
		if (userid==null){
			return JSONUtils.getRoot(-1,USER_LOGOUT,STR_IS_EMPTY);
		}
		return iTokenService.updateToken(account,password,userid);

	}

	@ApiOperation(value="删除凭证token", notes="删除凭证Token，删除之后，接口将请求不成功，因为没有凭证")
	@RequestMapping(value = "/delToken",method = RequestMethod.POST)
	public JSONObject delToken(Long tokenId){

		return	iTokenService.delToken(tokenId);

	}

	@ApiOperation(value="获取主题列表", notes="根据用户获取主题列表")
	@RequestMapping(value = "/getTopicList",method = RequestMethod.POST)
	public JSONObject getTopicList(QueryBean queryBean){
		Long userid =null;
		userid = (Long) request.getSession().getAttribute(USER_ID);
		if (userid==null){
			return JSONUtils.getRoot(-1,USER_LOGOUT,STR_IS_EMPTY);
		}
		JSONObject tokenlist = kafkaOperatorService.getTopicList(queryBean.getPageNo(),queryBean.getRowCount(),userid);

		return tokenlist;
	}

	@ApiOperation(value="查看主题", notes="查看主题的副本数量和分区数量等信息")
	@RequestMapping(value = "/getTopicInfo",method = RequestMethod.POST)
	public JSONObject getTopicInfo(Long topicId){

		JSONObject tokenlist = kafkaOperatorService.getTopicInfo(topicId);
		return tokenlist;

	}




	@RequestMapping(value = "/setRedisTest",method = RequestMethod.POST)
	public JSONObject setRedisTest(String str) {

		RedisUtil redisUtil = new RedisUtil();
		redisUtil.setRedisTemplate(redisTemplate);
		redisUtil.set("str",str,50);
		return JSONUtils.getRoot(0,SUCCESS_MSG,request.getSession().getId());
	}


	@RequestMapping(value = "/getSingle",method = RequestMethod.GET)
	public JSONObject getSingle(String str) {

		TokenBean tokenBean = new TokenBean();
		String restr =  str;
		System.out.println(request.getSession().getId() + ">>>>>>>>>>>>>>>>" + restr);
		System.out.println(request.getSession().getId() + ">>>>>>>>>>>>>>>>" + restr);

		tokenBean.setToken(restr);

		return JSONUtils.getRoot(0,SUCCESS_MSG,"sessionID = " + request.getSession().getId() + ",token = " + tokenBean.getToken());

	}

}